package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.service.SmartSettingsService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.menu.pojo.dto.MenuNewListDTO;
import com.xbongbong.pro.smartsettings.pojo.dto.SmartSettingsBusinessListDTO;
import com.xbongbong.pro.smartsettings.pojo.dto.SmartSettingsConditionValueDTO;
import com.xbongbong.pro.smartsettings.pojo.dto.SmartSettingsDelDTO;
import com.xbongbong.pro.smartsettings.pojo.dto.SmartSettingsEnableDTO;
import com.xbongbong.pro.smartsettings.pojo.dto.SmartSettingsGetDTO;
import com.xbongbong.pro.smartsettings.pojo.dto.SmartSettingsListDTO;
import com.xbongbong.pro.smartsettings.pojo.dto.SmartSettingsSaveDTO;
import com.xbongbong.pro.smartsettings.pojo.vo.SmartSettingsBusinessListVO;
import com.xbongbong.pro.smartsettings.pojo.vo.SmartSettingsConditionValueVO;
import com.xbongbong.pro.smartsettings.pojo.vo.SmartSettingsDelVO;
import com.xbongbong.pro.smartsettings.pojo.vo.SmartSettingsEnableVO;
import com.xbongbong.pro.smartsettings.pojo.vo.SmartSettingsGetVO;
import com.xbongbong.pro.smartsettings.pojo.vo.SmartSettingsListVO;
import com.xbongbong.pro.smartsettings.pojo.vo.SmartSettingsMenuListVO;
import com.xbongbong.pro.smartsettings.pojo.vo.SmartSettingsSaveVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @description: 智能助手
 * @Author: hongxiao
 * @date: 2019-04-23 20:17
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/aiAssistant")
public class AiAssistantController {
    @Resource
    private SmartSettingsService smartSettingsService;

    /**
     * 获取业务列表
     *
     * @param businessListDTO 入参
     * @param br
     * @return
     * @throws
     * @author hongxiao
     * @date: 2019-04-23 21:13
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/businessList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String businessList(@RequestBody @Valid SmartSettingsBusinessListDTO businessListDTO, BindingResult br) {
        XbbResponse<SmartSettingsBusinessListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmartSettingsBusinessListVO businessListVO = smartSettingsService.businessList(businessListDTO);
                response = new XbbResponse<>(businessListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    
    /**
     * 获取业务下智能助手列表
     *
     * @param listDTO 入参
     * @param br
     * @return
     * @throws
     * @author hongxiao
     * @date: 2019-04-23 21:14
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid SmartSettingsListDTO listDTO, BindingResult br) {
        XbbResponse<SmartSettingsListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmartSettingsListVO listVO = smartSettingsService.list(listDTO);
                response = new XbbResponse<>(listVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取设置
     *
     * @param getDTO 入参
     * @param br
     * @return
     * @throws
     * @author hongxiao
     * @date: 2019-04-23 21:15
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid SmartSettingsGetDTO getDTO, BindingResult br) {
        XbbResponse<SmartSettingsGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmartSettingsGetVO getVO = smartSettingsService.get(getDTO);
                response = new XbbResponse<>(getVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取触发器可用值
     *
     * @param conditionValueDTO 入参
     * @param br
     * @return
     * @throws
     * @author hongxiao
     * @date: 2019-04-23 21:18
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/getConditionValue", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getConditionValue(@RequestBody @Valid SmartSettingsConditionValueDTO conditionValueDTO, BindingResult br) {
        XbbResponse<SmartSettingsConditionValueVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmartSettingsConditionValueVO conditionValueVO = smartSettingsService.getConditionValue(conditionValueDTO);
                response = new XbbResponse<>(conditionValueVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 保存设置
     * 
     * @param saveDTO 入参
     * @param br
     * @return 
     * @throws 
     * @author hongxiao
     * @date: 2019-04-23 21:19
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid SmartSettingsSaveDTO saveDTO, BindingResult br) {
        XbbResponse<SmartSettingsSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmartSettingsSaveVO saveVO = smartSettingsService.save(saveDTO);
                response = new XbbResponse<>(saveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 启用/关闭智能助手
     * 
     * @param enableDTO 入参
     * @param br
     * @return
     * @throws 
     * @author hongxiao
     * @date: 2019-04-23 21:19
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/enable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String enable(@RequestBody @Valid SmartSettingsEnableDTO enableDTO, BindingResult br) {
        XbbResponse<SmartSettingsEnableVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmartSettingsEnableVO enableVO = smartSettingsService.enable(enableDTO);
                response = new XbbResponse<>(enableVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 智能助手-获取菜单列表
     *
     * @param menuListDTO 入参
     * @param br
     * @return
     * @throws
     * @author hongxiao
     * @date: 2019-04-27 20:16
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/menuList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String menuList(@RequestBody @Valid MenuNewListDTO menuListDTO, BindingResult br) {
        XbbResponse<SmartSettingsMenuListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmartSettingsMenuListVO menuListVO = smartSettingsService.menuList(menuListDTO);
                response = new XbbResponse<>(menuListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 删除智能助手
     *
     * @param smartSettingsDelDTO 入参
     * @param br
     * @throws
     * @return
     * @author hongxiao
     * @date 2019-11-14 19:26
     * @since
     * @version
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid SmartSettingsDelDTO smartSettingsDelDTO, BindingResult br) {
        XbbResponse<SmartSettingsDelVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmartSettingsDelVO smartSettingsDelVO = smartSettingsService.delete(smartSettingsDelDTO);
                response = new XbbResponse<>(smartSettingsDelVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
